using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace DigitalTrainSuite.UserControls
{
	public delegate bool ControlLocomotive(int locoID, int speed, bool changeDir, bool function);

	public partial class UserControlTrainContoller : UserControl
	{

	#region public events

		public event ControlLocomotive controlLocomotive;

		#endregion

	#region private members

		private bool going;
		private int locoID = -1; 

		#endregion

	#region public methods

		public int LocoID
		{ // Implement Error Checking
			get
			{
				return this.locoID;
			}
			set
			{
				this.locoID = value;
			}
		}

		public string LocoName
		{
			get
			{
				return this.labelTrainName.Text;
			}
			set
			{
				this.labelTrainName.Text = value;
				this.labelTrainName.Location = new Point((this.Width - this.labelTrainName.Width) / 2, this.labelTrainName.Location.Y);
			}
		}

		public UserControlTrainContoller()
		{
			InitializeComponent();
			going = false;
			this.buttonStopGo.BackColor = Color.Green;
			this.buttonStopGo.Text = "Go";
		} 

		#endregion

	#region private event handlers

		private void trackBarSpeed_Scroll(object sender, EventArgs e)
		{
			if (this.locoID == -1) return;
			if (going)
			{
				this.controlLocomotive(this.locoID, this.trackBarSpeed.Value, false, true);
			}
		}

		private void buttonStopGo_Click(object sender, EventArgs e)
		{
			if (this.locoID == -1) return;
			if (going)
			{
				this.buttonStopGo.BackColor = Color.Green;
				this.buttonStopGo.Text = "Go";
				this.controlLocomotive(this.locoID, 0, false, true);
				this.going = false;
			}
			else
			{
				this.buttonStopGo.BackColor = Color.Red;
				this.buttonStopGo.Text = "Stop";
				this.controlLocomotive(this.locoID, this.trackBarSpeed.Value, false, true);
				this.going = true;
			}
		}

		private void buttonReverse_Click(object sender, EventArgs e)
		{
			if (this.locoID == -1) return;
			// Add stop, reverse, go
			this.controlLocomotive(this.locoID, 0, true, true);
			this.trackBarSpeed.Value = 0;
		} 

		#endregion


	}
}
